<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
class Goods extends Common
{
    public function index()
    {

    }
	/*
	 *商品信息列表
	 */
	public function g_list(){
		$data = Db::table('goods') ->order('goods_addtime desc') -> paginate(10);
		$this -> assign('data',$data);
		return $this -> fetch();
	}
	/*
	 *添加商品信息
	 *第一步
	 */
	public function g_add(){
		$data = Request::instance()->Param();
		$res = '';
		if(!empty($data)){
			$res = Db::table('goods') -> where($data) -> find();
			$p_type = Db::table('goods_type')-> where('goods_type_id',$res['goods_b_type']) -> find();
			$this -> assign('p_type',$p_type);
		}
		$g_taste = Db::table('goods_taste_type') -> select();
		$g_type = Db::table('goods_type')-> where('goods_type_level',1) -> select();
		$this -> assign('res',$res);
		$this -> assign('g_taste',$g_taste);
		$this -> assign('g_type',$g_type);
		return $this -> fetch();
	}
	/*
	 *添加商品操作
	 */
	public function dog_add(){
		$param = Request::instance()->Param();
		$str = is_base64($param['goods_image']);
		if($str){
			$image = base64_img($param['goods_image']);
			if($image['error']){
				$img = $image['msg'];
			}else{
				return(array(
		            'error' => 1,
		            'msg'   => $image['msg']
		            ));
			}
			$data['goods_image'] = $img;
			$param['goods_image'] = $img;
			$old_img = Db::table('goods') -> where('goods_id',$param['goods_id']) -> find();
			if($old_img['goods_image']){
				unlink(ROOT_PATH  . 'public'.$old_img['goods_image']);
			}
			$g_tu = Db::table('goods_image') ->where('goods_id',$param['goods_id']) -> update(array('goods_image1'=>$data['goods_image']));
		}
		if(!$param['goods_id']){
			$data = $param;
			unset($data['goods_id']);
		}
		if($param['goods_id']){
			$result = Db::table('goods')-> where('goods_id',$param['goods_id']) -> update($param);
			if($result){
				 return(array(
	                'error' => 0,
	                'msg'   => '更新成功',
	                'url'   => '/admin/Goods/g_add/goods_id/'.$param['goods_id']
	                ));
	        } else {
				//错误页面的默认跳转页面是返回前一页，通常不需要设置
				return(array(
	                'error' => 1,
	                'msg'   => '更新失败',
	                'url'   => '/admin/Goods/g_add/goods_id/'.$param['goods_id']
	                ));
			}
		}
		$data['goods_image'] = $param['goods_image'];
		$data['goods_addtime'] = time();
		$result = Db::table('goods') -> insertGetId($data);
		$g_details = Db::table('goods_details') -> insert(array('goods_id'=>$result));
		if(!$g_details){
			$g_delete = Db::table('goods') -> where('goods_id',$result) -> delete();
			return(array(
                'error' => 1,
                'msg'   => '新增失败',
                'url'   => '/admin/Goods/g_add'
                ));
		}
		$g_tu = Db::table('goods_image') -> insert(array('goods_id'=>$result,'goods_image1'=>$data['goods_image']));
		if(!$g_tu){
			$g_delete = Db::table('goods') -> where('goods_id',$result) -> delete();
			$gd_delete = Db::table('goods_details') -> where('goods_id',$result) -> delete();
			return(array(
                'error' => 1,
                'msg'   => '新增失败',
                'url'   => '/admin/Goods/g_add'
                ));
		}
		$g_new = Db::table('goods') -> where('goods_id',$result) -> find();
		if($g_new){
			 return(array(
                'error' => 0,
                'msg'   => '新增成功<br/>请继续完善资料',
                'url'   => '/admin/Goods/g_add_details/id/'.$result
                ));
        } else {
			//错误页面的默认跳转页面是返回前一页，通常不需要设置
			return(array(
                'error' => 1,
                'msg'   => '新增失败',
                'url'   => '/admin/Goods/g_add'
                ));
		}
	}
	/**
	 * 添加商品详情
	*/
	public function g_add_details(){
		$data = Request::instance()->Param();
		$g_info = Db::table('goods') -> where('goods_id',$data['id']) -> find();
		$gd_info = Db::table('goods_details') -> where('goods_id',$data['id']) -> find();
		$this -> assign('g_info',$g_info);
		$this -> assign('res',$gd_info);
		return $this -> fetch();
	}
	public function dog_add_deta(){
		$data = Request::instance() -> Param();
		$result = Db::table('goods_details')
				-> where('goods_id',$data['goods_id'])
				-> update($data);
		if($result){
			return(array(
				'error' => 0,
				'msg'   => '更新成功<br/>请继续完善资料',
				'url'   => '/admin/Goods/g_add_tu/id/'.$data['goods_id']
			));
		} else {
			//错误页面的默认跳转页面是返回前一页，通常不需要设置
			return(array(
				'error' => 1,
				'msg'   => '更新失败',
				'url'   => '/admin/Goods/g_add_details/id/'.$data['goods_id']
			));
		}
	}
	/**
	 * 添加商品图片
	*/
	public function g_add_tu(){
		$data = Request::instance() -> Param();
		$res = Db::table('goods_image') -> where('goods_id',$data['id']) -> find();
		$this->assign('res',$res);
		return $this->fetch();
	}
	public function dog_add_tu(){
		$data = Request::instance() -> Param();
		$image = $data;
		unset($image['goods_id']);
		$count = count($image);
		if($count > 3){
			return(array(
				'error' => 1,
				'msg'   => '请上传不多于3张图片',
				'url'   => '/admin/Goods/g_add_tu/id/'.$data['goods_id']
			));
		}
		$img = array();
		foreach($image as $k => $v){
			$photo = base64_img($v);
			if(!$photo['error']){
				return(array(
					'error' => 1,
					'msg'   => $photo['msg']
				));
			}
			$img[$k] = $photo['msg'];
			$old_img = Db::table('goods_image') -> where('goods_id',$data['goods_id']) -> find();

			if($old_img[$k]){
				unlink(ROOT_PATH  . 'public'.$old_img[$k]);
			}
		}

		$result = Db::table('goods_image') -> where('goods_id',$data['goods_id']) -> update($img);
		if($result){
			return(array(
				'error' => 0,
				'msg'   => '更新成功',
				'url'   => '/admin/Goods/g_list/'
			));
		} else {
			//错误页面的默认跳转页面是返回前一页，通常不需要设置
			return(array(
				'error' => 1,
				'msg'   => '更新失败',
				'url'   => '/admin/Goods/g_add_tu/id/'.$data['goods_id']
			));
		}


	}
	/*
	 *删除商品信息
	 */
	public function g_delete(){
		$data = Request::instance()->Param();
		//判断是否是活动商品
		$sel = Db::table('goods_activity') -> where($data) -> find();
		if($sel){
			return (array(
				'error' => 1,
				'msg'   => '请先取消活动',
				'url'   => '/admin/Goods/g_list'
				));
		}
		//删除商品图片
		$del_tu = Db::table('goods_image') -> where($data) -> delete();
		if(!$del_tu){
			return (array(
				'error' => 1,
				'msg'   => '商品图片删除失败',
				'url'   => '/admin/Goods/g_list'
				));
		}
		//删除商品详情
		$del_deta = Db::table('goods_details') -> where($data) -> delete();
		if(!$del_tu){
			return (array(
				'error' => 1,
				'msg'   => '商品详情删除失败',
				'url'   => '/admin/Goods/g_list'
				));
		}
		$result = Db::table('goods') -> where($data) -> delete();
		if($result){
			return (array(
				'error' => 0,
				'msg'   => '商品删除成功',
				'url'   => '/admin/Goods/g_list'
				));
        } else {
			return (array(
				'error' => 1,
				'msg'   => '商品删除失败',
				'url'   => '/admin/Goods/g_list'
				));
		}
	}
	/*
	 *查看商品信息
	 */
	public function g_select(){
		$data = Request::instance()->Param();
		$res = Db::table('goods') -> where($data) -> find();
		$res_d = Db::table('goods_details') -> where($data) -> find();
		$t1 = Db::table('goods_type') -> where('goods_type_id',$res['goods_a_type']) -> find();
		$t2 = Db::table('goods_type') -> where('goods_type_id',$res['goods_b_type']) -> find();
		$this -> assign('res',$res);
		$this -> assign('t1',$t1);
		$this -> assign('t2',$t2);
		$this -> assign('res_d',$res_d);
		return $this -> fetch();
	}
	/*
	 *审核商品操作
	 */
	public function g_check(){
		$data = Request::instance()->Param();
		$result = Db::table('goods') -> where($data) -> update(array('member_check' => 2,'member_status' => 1));
		if($result){
			//设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
			$this->success('审核成功', 'Member/m_list');
        } else {
			//错误页面的默认跳转页面是返回前一页，通常不需要设置
			$this->error('审核失败');
		}
	}
	/*
	 * 商品二级分类
	 */
	public function AjaxType(){
		$data = Request::instance() -> Param();
		$g_type = Db::table('goods_type')-> where('goods_type_pid',$data['id']) -> select();
		return $g_type;
	}
	/**
	 * 修改商品状态
	*/
	public function isActivity(){
		$data = Request::instance()-> Param();
		if($data['goods_status']){
			$data['goods_status'] = 0;
			$result = Db::table('goods') -> where('goods_id',$data['goods_id']) -> update(array('goods_isactivity'=>$data['goods_status']));
			Db::table('goods_activity') ->where(array('goods_id'=>$data['goods_id'])) -> delete();
		}else{
			$data['goods_status'] = 1;
			$result = Db::table('goods') -> where('goods_id',$data['goods_id']) -> update(array('goods_isactivity'=>$data['goods_status']));
			Db::table('goods_activity') -> insert(array('goods_id'=>$data['goods_id']));
		}
		if($result){
			return(array(
				'error' => 0,
				'msg'   => '修改成功',
				'url'   => '/admin/Goods/g_list/'
			));
		} else {
			//错误页面的默认跳转页面是返回前一页，通常不需要设置
			return(array(
				'error' => 1,
				'msg'   => '修改失败',
				'url'   => '/admin/Goods/g_list/'
			));
		}
	}
	/**
	 * 修改商品推荐
	 */
	public function goods_recom(){
		$data = Request::instance()-> Param();
		if($data['goods_status']){
			$data['goods_status'] = 0;
			$result = Db::table('goods') -> where('goods_id',$data['goods_id']) -> update(array('goods_recom'=>$data['goods_status']));
		}else{
			$data['goods_status'] = 1;
			$result = Db::table('goods') -> where('goods_id',$data['goods_id']) -> update(array('goods_recom'=>$data['goods_status']));
		}
		if($result){
			return(array(
				'error' => 0,
				'msg'   => '修改成功',
				'url'   => '/admin/Goods/g_list/'
			));
		} else {
			//错误页面的默认跳转页面是返回前一页，通常不需要设置
			return(array(
				'error' => 1,
				'msg'   => '修改失败',
				'url'   => '/admin/Goods/g_list/'
			));
		}
	}
	/**
	 *活动商品列表
	*/
	public function g_activity(){
		$act = Db::table('goods_activity')
			-> alias('ga')
			-> join('goods_activity_type gat','ga.activity_type_id = gat.activity_type_id')
			-> paginate(10);
		$this->assign('act',$act);
		return $this->fetch();
	}
	/*
	 *活动商品编辑
	 */
	public function g_edit_activity(){
		$data = Request::instance() -> Param();
		$res = Db::table('goods_activity')
		-> where('goods_id',$data['goods_id'])
		-> find();
		$combo = Db::table('goods_combo') -> select();
		$activity_t = Db::table('goods_activity_type') -> select();

		$this -> assign('res',$res);
		$this -> assign('combo',$combo);
		$this -> assign('activity_t',$activity_t);
		return $this->fetch();
	}
	public function dog_edit_activity(){
		$data = Request::instance() -> Param();
		$data['goods_start'] = strtotime($data['goods_start']);
		$data['goods_end'] = strtotime($data['goods_end']);

		$result = Db::table('goods_activity')
			-> where('goods_id',$data['goods_id'])
			-> update($data);

		if($result){
			return(array(
				'error' => 0,
				'msg'   => '修改成功',
				'url'   => '/admin/Goods/g_activity/'
			));
		} else {
			//错误页面的默认跳转页面是返回前一页，通常不需要设置
			return(array(
				'error' => 1,
				'msg'   => '修改失败',
				'url'   => '/admin/Goods/g_edit_activity/id/'.$data['goods_id']
			));
		}
	}
	/*
	*商品分类
	 */
	public function g_type_list(){
		$data = Db::table('goods_type') -> paginate(10);
		$this->assign('data',$data);
		return $this->fetch();
	}
	public function g_type(){
		$data = Request::instance() -> Param();
		$res = '';
		if($data){
			$res = Db::table('goods_type') ->where($data) -> find();

		}
		$g_type = Db::table('goods_type') -> where('goods_type_level',1) -> select();
		$this -> assign('g_type',$g_type);
		$this -> assign('res',$res);
		return $this -> fetch();
	}
	public function dog_type(){
		$param = Request::instance() -> Param();
		$param['goods_type_time'] = time();
		$str = is_base64($param['goods_type_icon']);
		if($str){
			$image = base64_img($param['goods_type_icon']);
			if($image['error']){
				$img = $image['msg'];
			}else{
				return(array(
					'error' => 1,
					'msg'   => $image['msg']
				));
			}
			$data['goods_type_icon'] = $img;
			$param['goods_type_icon'] = $img;
			$old_img = Db::table('goods_type') -> where('goods_type_id',$param['goods_type_id']) -> find();
			if($old_img['goods_type_icon']){
				unlink(ROOT_PATH  . 'public'.$old_img['goods_type_icon']);
			}
		}
		if($param['goods_type_id']){
			$result = Db::table('goods_type') -> where('goods_type_id',$param['goods_type_id']) -> update($param);
			if($result){
				return(array(
					'error' => 0,
					'msg'   => '编辑成功',
					'url'   => '/admin/Goods/g_type_list'
				));
			} else {
				//错误页面的默认跳转页面是返回前一页，通常不需要设置
				return(array(
					'error' => 1,
					'msg'   => '编辑失败',
					'url'   => '/admin/Goods/g_type'
				));
			}
		}
		$data = $param;
		unset($data['goods_type_id']);
		if(!$data['goods_type_pid']){
			$data['goods_type_level'] = 1;
		}else{
			$data['goods_type_level'] = 2;
		}
		$result = Db::table('goods_type') -> insert($data);
		if($result){
			return(array(
				'error' => 0,
				'msg'   => '添加成功',
				'url'   => '/admin/Goods/g_type_list'
			));
		} else {
			//错误页面的默认跳转页面是返回前一页，通常不需要设置
			return(array(
				'error' => 1,
				'msg'   => '添加失败',
				'url'   => '/admin/Goods/g_type'
			));
		}
	}
	public function gt_delete(){
		$data = Request::instance()-> Param();
		$t = Db::table('goods_type') -> where('goods_type_pid',$data['goods_type_id']) -> select();
		if($t){
			return(array(
				'error' => 1,
				'msg'   => '请先删除二级分类'
			));
		}
		$result = Db::table('goods_type')-> where('goods_type_id',$data['goods_type_id']) ->delete();
		if($result){
			return(array(
				'error' => 0,
				'msg'  => '删除成功',
				'url'  => '/admin/Goods/g_type_list'
			));
		}else{
			return(array(
				'error' => 1,
				'msg'  => '删除失败',
				'url'  => '/admin/Goods/g_type_list'
			));

		}

	}
	/*
	*添加套餐
	 */
	public function combo(){
		$data = Request::instance() -> Param();
		return $this->fetch();
	}
	public function doCombo(){
		$data = Request::instance() -> Param();
		$data['combo_addtime'] = time();
		$result = Db::table('goods_combo') -> insert($data);
		if($result){
			return(array(
				'error' => 0,
				'msg'   => '添加成功',
				'url'   => '/admin/Goods/g_list'
			));
		} else {
			//错误页面的默认跳转页面是返回前一页，通常不需要设置
			return(array(
				'error' => 1,
				'msg'   => '添加失败',
				'url'   => '/admin/Goods/combo'
			));
		}
	}
}

